#include <bits/stdc++.h>
#define rep(i,n) for(int i=0,del##i##verme=int(n);i<del##i##verme;++i)
#define rep1(i,n) for(int i=1,parano##i##a=int(n);i<=parano##i##a;++i)
#define per(i,n) for(int i=int(n)-1;i>=0;--i)
#define per1(i,n) for(int i=int(n);i>=1;--i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define y0 Lingluo
#define y1 VividCycle
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
const ll mod1=998244353;
const ll mod2=1000000007;
const ldb pi=3.14159265358979323846264338327950;
unsigned time_related_rand()
{
    return unsigned(chrono::steady_clock::now().time_since_epoch().count());
}
int n,m,k,c,q,x[4];
int a[132005];
int v[132005];
string s[32];
vector<int> qid[100005];
int dep[262144];
int mn[262144];
int det[262144];
bool mx[262144];
bool prop=true;
void dfs(int ver,int l,int r,int bd)
{
    if(l==r)
    {
        mn[ver]=(r<=bd)?(v[l]):0;
        mx[ver]=(r<=bd);
    }
    else
    {
        int m=(l+r)>>1;
        dfs(ver<<1,l,m,bd);
        dfs(ver<<1|1,m+1,r,bd);
        if(s[dep[ver]][ver-(1<<(k-dep[ver]))]=='0')
        {
            if(mn[ver<<1]>=dep[ver])
            mn[ver]=mn[ver<<1],mx[ver]=mx[ver<<1];
            else if(mx[ver<<1])
            mn[ver]=mn[ver<<1|1],mx[ver]=mx[ver<<1|1];
            else
            mn[ver]=min(mn[ver<<1|1],dep[ver]),mx[ver]=false;
        }
        else
        {
            if(mn[ver<<1|1]>=dep[ver])
            mn[ver]=mn[ver<<1|1],mx[ver]=mx[ver<<1|1];
            else if(mx[ver<<1|1])
            mn[ver]=mn[ver<<1],mx[ver]=mx[ver<<1];
            else
            mn[ver]=min(mn[ver<<1],dep[ver]),mx[ver]=false;
        }
    }
  //  cout<<ver<<" "<<mn[ver]<<" "<<mx[ver]<<endl;
}
void solve(int ver,int l,int r)
{
    if(l==r) det[ver]=l;
    else
    {
        int m=(l+r)>>1;
        solve(ver<<1,l,m);
        solve(ver<<1|1,m+1,r);
        if(s[dep[ver]][ver-(1<<(k-dep[ver]))]=='0')
        {
            if(v[det[ver<<1]]>=dep[ver]) det[ver]=det[ver<<1];
            else det[ver]=det[ver<<1|1];
        }
        else
        {
            if(v[det[ver<<1|1]]>=dep[ver]) det[ver]=det[ver<<1|1];
            else det[ver]=det[ver<<1];
        }
    }
}
ll ans;
void Q()
{
    cin>>x[0]>>x[1]>>x[2]>>x[3];
    ans=0;
    int cur=1<<k;
    for(int i=1;i<=cur;++i) dep[i+cur-1]=0;
    for(int i=cur-1;i>=1;--i) dep[i]=dep[i<<1]+1;
    rep1(i,n) v[i]=a[i]^x[i&3];
    
    if(n<=5000&&!prop)
    {
        int x=1,curt=1<<k;
        rep1(i,n)
        {
            if(x<i) x<<=1,curt>>=1;
            dfs(curt,1,x,i);
            ll result=0;
            rep1(j,x)
            {
                int val=(j<=i)?v[j]:114514;
                int vert=j+(1<<k)-1;
                bool fl=true;
                while(vert!=curt)
                {
                    if((s[dep[vert]+1][(vert>>1)-(1<<(k-dep[vert>>1]))]&1)==(vert&1))
                    {
                  //      cout<<" ("<<val<<" "<<vert<<" "<<dep[vert>>1]<<") ";
                        if(val<dep[vert>>=1]) {fl=false;break;}
                    }
                    else
                    {
                 //       cout<<" ["<<mn[vert^1]<<" "<<(vert^1)<<" "<<dep[vert>>1]<<"] ";
                        if(mn[vert^1]>=dep[vert>>1]) {fl=false;break;}
                        vert>>=1;
                    }
                }
                if(fl) result+=j;
              //  if(fl) cout<<j<<" ";
              //  else cout<<"NOT"<<j<<" ";
            }
       //     cout<<"/ "<<i<<endl;
            for(int id:qid[i]) ans^=result*id;
        }
        cout<<ans<<'\n';
        return ;
    }

    {
        int x=1,curt=1<<k;
        solve(1,1,1<<k);
        rep1(i,n)
        {
            if(x<i) x<<=1,curt>>=1;
            ll result=det[curt];
            for(int id:qid[i]) ans^=result*id;
        }
        cout<<ans<<'\n';
        return ;
    }
}
int main()
{
    #ifndef DEBUG
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);cin.tie(0);
    cin>>n>>m;while((1<<k)<n)++k;
    rep1(i,n) cin>>a[i];
    rep1(i,m)
    {
        cin>>c;qid[c].pb(i);
        if(c&(c-1)) prop=false;
    }
    rep1(i,k) cin>>s[i];
    cin>>q;while(q--)Q();
    return 0;
}
/*
1. check file name, corner case, i or j, < or <=, x or y, p or q, > or >=, c or d, n or m, n or q, overflow
2. time / memory: by ulimit -v LIMIT(kb) and ulimit -s 1048576 (use time ./prog , size prog or usr/bin/time -v prog)
*/
/*
maybe sonner or later, I can fall in love with a leaf
*/